import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import tailwindcss from '@tailwindcss/vite'
import { nodePolyfills } from 'vite-plugin-node-polyfills'

// https://vite.dev/config/
export default defineConfig({
  base: './', // 使用相对路径
  plugins: [
    vue(), 
    tailwindcss(),
    nodePolyfills({
      // 启用所有 polyfills
      include: ['process', 'buffer', 'crypto', 'stream', 'util'],
      // 全局启用
      globals: {
        Buffer: true,
        global: true,
        process: true,
      },
    })
  ],
  define: {
    global: 'globalThis',
  },
  optimizeDeps: {
    include: ['otplib']
  },
  build: {
    // 构建配置
    outDir: 'dist',
    assetsDir: 'assets',
    // 确保资源使用相对路径
    rollupOptions: {
      output: {
        // 确保资源文件使用相对路径
        assetFileNames: 'assets/[name]-[hash][extname]',
        chunkFileNames: 'assets/[name]-[hash].js',
        entryFileNames: 'assets/[name]-[hash].js'
      }
    }
  },
  resolve: {
    alias: {
      '#': path.resolve(__dirname, 'src'),
      '#/views': path.resolve(__dirname, 'src/views'),
      '#/components': path.resolve(__dirname, 'src/components'),
      '#/utils': path.resolve(__dirname, 'src/utils'),
      '#/server': path.resolve(__dirname, 'src/server'),
      '#/store': path.resolve(__dirname, 'src/store'),
      '#/router': path.resolve(__dirname, 'src/router'),
      '#/assets': path.resolve(__dirname, 'src/assets'),
      '#/constants': path.resolve(__dirname, 'src/constants'),
      '#/types': path.resolve(__dirname, 'src/types'),
    }
  },
  server: {
    proxy: {
      '/api/xxl-job': {
        target: 'http://121.43.113.53:9001',
        changeOrigin: true,
        secure: false,
        rewrite: (path) => path.replace(/^\/api\/xxl-job/, '/xxl-job-admin'),
        configure: (proxy) => {
          proxy.on('proxyReq', (proxyReq) => {
            // 添加认证Cookie到代理请求
            proxyReq.setHeader('Cookie', 'XXL_JOB_LOGIN_IDENTITY=7b226964223a322c22757365726e616d65223a2277616e6779616e6762696e222c2270617373776f7264223a223062333433663034623235326335633933656163306632393461366565353264222c22726f6c65223a302c227065726d697373696f6e223a2235227d');
          });
        }
      }
    }
  }
})
